﻿using System;
using System.Globalization;

namespace JanusLibAing.AutoSave
{
    /// <summary>
    /// Stored enum name
    /// </summary>
    public abstract class DataItemEnum : IDataItem
    {
        private Enum enumValue;

        /// <summary>
        /// Initializes a new instance of the <see cref="DataItemString"/> class.
        /// </summary>
        /// <param name="value">The value.</param>
        protected DataItemEnum(Enum value)
        {
            enumValue = value;
        }

        /// <summary>
        /// Returns a <see cref="System.String"/> that represents this instance.
        /// </summary>
        /// <returns>
        /// A <see cref="System.String"/> that represents this instance.
        /// </returns>
        public override string ToString()
        {
            return ItemValue.ToString();
        }

        public void ParseValue(string importValue)
        {
            Type enumType = enumValue.GetType();
            if (Enum.IsDefined(enumType, importValue))
            {
                enumValue = (Enum) Enum.Parse(enumType, importValue);
            }
        }

        /// <summary>
        /// Gets the item name.
        /// </summary>
        /// <value>The name.</value>
        public abstract string Name { get; }

        /// <summary>
        /// Gets or sets the value.
        /// </summary>
        /// <value>The value.</value>
        public object Value
        {
            get { return enumValue; }
            set { enumValue = (Enum)value; }
        }

        /// <summary>
        /// Gets or sets the item value.
        /// </summary>
        /// <value>The item value.</value>
        public Enum ItemValue
        {
            get { return enumValue; }
            set { enumValue = value; }
        }
    }
}